package edu.zqxy.eastmoney.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.zqxy.eastmoney.dto.PageDto;
import edu.zqxy.eastmoney.mapper.StockMapper;
import edu.zqxy.eastmoney.pojo.Stock;
import edu.zqxy.eastmoney.pojo.User;
import edu.zqxy.eastmoney.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class StockServiceImpl implements IStockService {
@Autowired
    StockMapper stockMapper;//...

    @Override
    public int save(Stock stock)
    {
        return stockMapper.insert(stock);
    }

    @Override
    public int update(Stock stock)
    {
        return stockMapper.updateByPrimaryKey(stock);
    }

    @Override
    public int delete(Object code)
    {
        return stockMapper.deleteByPrimaryKey(code);
    }

    @Override
    public Stock findById(Object code)
    {
        return stockMapper.selectByPrimaryKey(code);
    }

    @Override
    public List<Stock> findAll()
    {
        return stockMapper.selectAll();
    }

    @Override
    public List<Stock> findByPinyin(String pinyin) {
        //底层的mapper没有提供默认的方法，此项需要自己写逻辑
        Example example=new Example(Stock.class);
        example.setForUpdate(true);
        //进行模糊查询
        example.createCriteria().andLike("pinyin","%"+pinyin+"%");
       //返回最终查询结果
        return stockMapper.selectByExample(example);
    }

    @Override
    public PageDto<Stock> findAllPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Stock> pageUsers =(Page<Stock> ) stockMapper.selectAll();

        PageDto<Stock> pageDto=new PageDto();
        pageDto.setCurPageNo(pageNo);
        pageDto.setPageSize(pageSize);
        pageDto.setData(pageUsers.getResult());
        pageDto.setTotal(pageUsers.getTotal());


        return pageDto;

    }

}
